package com.heima.xxljob.job;

import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class DemoJob {

    @XxlJob("demo")
    public void sayHello(){
        System.out.println("hello.....");
    }

    @XxlJob("sharding")
    public void sharding(){
        List<Integer> list = getList();
        //获取总的分配数量
        int shardTotal = XxlJobHelper.getShardTotal();
        //当前任务的分片
        int shardIndex = XxlJobHelper.getShardIndex();
        for (int i=0; i < list.size(); i++){
            //自定义分片策略
            if(i % shardTotal == shardIndex){
                System.out.println(list.get(i));
            }
        }
    }

    public List<Integer> getList(){
        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < 10000; i++) {
            list.add(i);
        }
        return list;
    }
}
